package com.guangzhi.weijianzhi.activity;

import org.apache.http.Header;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.guangzhi.weijianzhi.R;
import com.guangzhi.weijianzhi.company.center.CompileCompanyInfoActivity;
import com.guangzhi.weijianzhi.context.WeiJianZhiEntity;
import com.guangzhi.weijianzhi.http.HttpRequestUtils;
import com.guangzhi.weijianzhi.http.MyAsyncHttpResponseHandler;
import com.guangzhi.weijianzhi.view.LodingFragmentDialog;

import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;
/**
 * 身份选择页面
 * @author Administrator
 *
 */
public class UserAndCompanyActivity extends BaseActivity implements OnClickListener {

	private String mToken;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		mToken = getIntent().getStringExtra("token");
		setContentView(R.layout.tz_user_comp_layout);
		initView();
	}

	private void initView() {
		// TODO Auto-generated method stub
		initTitle();
		
		findViewById(R.id.company).setOnClickListener(this);
		findViewById(R.id.user).setOnClickListener(this);
		goBack.setCompoundDrawables(null, null, null, null);
	//	goBack.setOnClickListener(this);
		titleTextV.setText("身份选择页面");
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.company:
			if(mToken!=null && !"".equals(mToken)){
			isUser("C");
			}
			break;
		case R.id.user:
			if(mToken!=null && !"".equals(mToken)){
				isUser("U");
			}
			break;

		}
	}

	private void isUser(final String string) {
		// TODO Auto-generated method stub
		HttpRequestUtils.doHttpRegisterSelect(mToken, string, new MyAsyncHttpResponseHandler(getSupportFragmentManager(),getResources()) {
			
			@Override
			public void onSuccess(int arg0, Header[] arg1, byte[] arg2) {
				// TODO Auto-generated method stub
				super.onSuccess(arg0, arg1, arg2);
				
				try {
					JSONObject jsonO = new JSONObject(new String(arg2));
					
					if(jsonO.optBoolean("status")){//{"data":{"result":"修改成功!","userHasCompany":"N"},"errMessage":"","status":true}
						WeiJianZhiEntity.userType =string;
						WeiJianZhiEntity.access_token =mToken;
						if("U".equals(string)){//如果选择的是个人
							JSONObject optJSONObject = jsonO.optJSONObject("data");
							Toast.makeText(UserAndCompanyActivity.this,optJSONObject.optString("result") , 0).show();
							WeiJianZhiEntity.isLogin =1;
							startActivity(new Intent(UserAndCompanyActivity.this, MainPageActivity.class));
							
						}else{//如果选择的是公司
							JSONObject optJSONObject = jsonO.optJSONObject("data");
							if("Y".equals(optJSONObject.optString("userHasCompany"))){//如果有公司信息
								WeiJianZhiEntity.isLogin =1;
								startActivity(new Intent(UserAndCompanyActivity.this, MainPageActivity.class));
								
							}else{//如果没有公司信息
								//todo跳转到公司注册界面
								
								Intent intent = new Intent(UserAndCompanyActivity.this, CompileCompanyInfoActivity.class);
								intent.putExtra("select", true);
								startActivity(intent);
							}
							
						}
						finish();
					}
					
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
			}
			
			@Override
			public void onFinish() {
				// TODO Auto-generated method stub
				super.onFinish();
				LodingFragmentDialog.dismiss(getSupportFragmentManager());
			}
			
		});
	}
    @Override  
    public boolean onKeyDown(int keyCode, KeyEvent event)  
    {  
        if (keyCode == KeyEvent.KEYCODE_BACK )  
        {  
        	   new  AlertDialog.Builder(this)    
      	     
               .setTitle("微兼职" )  
  
               .setMessage("请选择您的身份" )  
  
               .setPositiveButton("知道了" ,  null )  
 
               .show();
  
        }  
          
        return false;  
          
    }  
	
}
